<?php

defined('WEBWI_ROOT_PATH') or die('Restricted access');
/* 
    @ PHP5 Mysql-klasse  
    @ Copyright by Web Communication World (www.wccw.in) 
    @ Diese Klasse darf frei unter diesem Vermerk eingesetzt, verändert und weitergegeben werden 

    Verwendete Funktionen: 
    @ getFieldsNum 
    @ genId // will use auto_increment 
    @ fetchRow // return mysql_fetch_row 
    @ fetchBoth  // return mmysql_fetch_array ;param = MYSQL_BOTH
    @ fetchArray // return mysql_fetch_assoc
    @ fetchObject // returns mysql_fetch_object
    @ getInsertId // returns mysql_insert_id 
    @ getRowsNum  // return mysql_num_rows
    @ getAffectedRows // return  mysql_affected_rows
    @ close // return mysql_close
    @ freeRecordSet // return mysql_free_result
    @ error // return mysql_error
    @ errno // return mysql_errno
    @ quoteString // return this->quote // to safty insert a string with qoutes
    @ quote // returns a qoutedt String 
	@ queryF // performs a query an the Database
	@ query // perfom a query
	@ queryFromFile // performs a query from an sql file
	@ getFieldName // return mysql_field_name
	@ getFieldType // return mysql_field_type
	@ getFieldsNum // return mysql_num_fields
*/ 
include_once WEBWI_ROOT_PATH . '/classes/database.php';


class WebwiMySQLDatabase extends WebwiDatabase
{
   
    private $conn;

    function connect($selectdb = true)
    {
        static $db_charset_set;

        if (!extension_loaded('mysql')) {
            trigger_error('notrace:mysql extension not loaded', E_USER_ERROR);
            return false;
        }

        $this->allowWebChanges = ($_SERVER['REQUEST_METHOD'] != 'GET');

        if (WEBWI_DB_PCONNECT == 1) {
            $this->conn = @mysql_pconnect(WEBWI_DB_HOST, WEBWI_DB_USER, WEBWI_DB_PASS);
        } else {
            $this->conn = @mysql_connect(WEBWI_DB_HOST, WEBWI_DB_USER, WEBWI_DB_PASS);
        }

        if (!$this->conn) {
            $this->logger->addQuery('', $this->error(), $this->errno());
            return false;
        }
        if ($selectdb != false) {
            if (!mysql_select_db(WEBWI_DB_NAME)) {
                $this->logger->addQuery('', $this->error(), $this->errno());
                return false;
            }
        }
        if (!isset($db_charset_set) && defined('WEBWI_DB_CHARSET') && WEBWI_DB_CHARSET) {
            $this->queryF("SET NAMES '".WEBWI_DB_CHARSET."'");
        }
        $db_charset_set = 1;
        $this->queryF("SET SQL_BIG_SELECTS = 1");
        return true;
    }


    function genId($sequence)
    {
        return 0; // * benutzt autoincrement
    }

    function fetchRow($result)
    {
        return @mysql_fetch_row($result);
    }

    function fetchArray($result)
    {
        return @mysql_fetch_assoc($result);
    }
	

    function fetchBoth($result)
    {
        return @mysql_fetch_array($result, MYSQL_BOTH);
    }

    function fetchObject($result)
    {
        return @mysql_fetch_object($result);
    }

    function getInsertId()
    {
        return mysql_insert_id($this->conn);
    }

    function getRowsNum($result)
    {
        return @mysql_num_rows($result);
    }

    function getAffectedRows()
    {
        return mysql_affected_rows($this->conn);
    }

    function close()
    {
        mysql_close($this->conn);
    }


    function freeRecordSet($result)
    {
        return mysql_free_result($result);
    }

    function error()
    {
        return @mysql_error();
    }

    function errno()
    {
        return @mysql_errno();
    }

    function quoteString($str)
    {
        return $this->quote($str);
    }

    function quote($string)
    {
        return "'" . str_replace("\\\"", '"', str_replace("\\&quot;", '&quot;', mysql_real_escape_string($string, $this->conn))) . "'";
    }

    function queryF($sql, $limit = 0, $start = 0)
    {
        if (!empty($limit)) {
            if (empty($start)) {
                $start = 0;
            }
            $sql = $sql . ' LIMIT ' . (int) $start . ', ' . (int) $limit;
        }
        //$this->logger->startTime('query_time');
        $result = mysql_query($sql, $this->conn);
        //$this->logger->stopTime('query_time');
       //$query_time = $this->logger->dumpTime('query_time', true);
        if ($result) {
            //$this->logger->addQuery($sql, null, null, $query_time);
            return $result;
        } else {
            //$this->logger->addQuery($sql, $this->error(), $this->errno(), $query_time);
            return false;
        }
    }


    function query($sql, $limit = 0, $start = 0)
    {
    }

    function queryFromFile($file)
    {
        if (false !== ($fp = fopen($file, 'r'))) {
            include_once WEBWI_ROOT_PATH . '/system/classes/sqlutility.php';
            $sql_queries = trim(fread($fp, filesize($file)));
            SqlUtility::splitMySqlFile($pieces, $sql_queries);
            foreach ($pieces as $query) {
                // [0] contains the prefixed query
                // [4] contains unprefixed table name
                $prefixed_query = SqlUtility::prefixQuery(trim($query), $this->prefix());
                if ($prefixed_query != false) {
                    $this->query($prefixed_query[0]);
                }
            }
            return true;
        }
        return false;
    }

     // * Get field name
    function getFieldName($result, $offset)
    {
        return mysql_field_name($result, $offset);
    }


     // * Get field type
    function getFieldType($result, $offset)
    {
        return mysql_field_type($result, $offset);
    }


     // * Get number of fields in result
    function getFieldsNum($result)
    {
        return mysql_num_fields($result);
    }
}


class WebwiMySQLDatabaseSafe extends WebwiMySQLDatabase
{

    // * perform a query on the database
    function query($sql, $limit = 0, $start = 0)
    {
        return $this->queryF($sql, $limit, $start);
    }
}


?>